package com.z8fz.admin.food.action;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.z8fz.admin.food.model.Food;
import com.z8fz.admin.food.model.Method;
import com.z8fz.admin.food.model.Type;
import com.z8fz.admin.food.service.FoodService;
import com.z8fz.admin.food.service.FoodTypeService;
import com.z8fz.admin.food.service.MethodService;
import com.z8fz.common.BaseAction;
import com.z8fz.common.pager.PageModel;

/**
 * 
 * @author bao zhiyao
 *
 */
public class FoodAction extends BaseAction {
	
	
	
	public String index(){
		PageModel pm = foodService.findFoods();
		ActionContext.getContext()
				.put("pm", pm);
		
		return SUCCESS;
	}
	
	public String add(){
		PageModel pm = foodService.findType();
		ActionContext.getContext().put("pm", pm);
		return SUCCESS;
	}
	public String addMethod(){
		Food food = foodService.findFoodById(id);
		Integer maxStep = methodService.findMaxStep(id);
		ActionContext.getContext().put("food", food);
		ActionContext.getContext().put("maxStep", maxStep);
		return SUCCESS;
	}
	
//	�����
	public String submitMethod()
	{	
		ServletContext servletContext = ServletActionContext.getServletContext();
		String targetDirectory = servletContext.getRealPath("/image/web/upload/method/");    
        String targetFileName = generateFileName(imageFileName);    
        File target = new File(targetDirectory, targetFileName);    
            
        try {
			FileUtils.copyFile(image, target);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    
		String contextPath = servletContext.getContextPath();
		method.setMethodImage(contextPath+"/image/web/upload/method/"+targetFileName);
		methodService.add(method);
		//����food��ֵ
		setId(method.getFoodId());
		//���ص����ҳ��
		return "toInitAdd";
	}
	
	public String addFood(){
		//���ز�Ʒ����
		Type type =  foodTypeService.findTypeById(food.getTypeId());
		//���ò�Ʒ����
		food.setTbType(type);
		//�õ�servletContext
		ServletContext servletContext = ServletActionContext.getServletContext();
		String targetDirectory = servletContext.getRealPath("/image/web/upload/food");    
        String targetFileName = generateFileName(imageFileName);    
        File target = new File(targetDirectory, targetFileName);    
            
        try {
			FileUtils.copyFile(image, target);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    
		String contextPath = servletContext.getContextPath();
		food.setFoodImage(contextPath+"/image/web/upload/food/"+targetFileName);
		foodService.addFood(food);
		return SUCCESS;
	}
	
//	�ļ��ϴ�,�����ļ���
	private String generateFileName(String fileName) {    
        DateFormat format = new SimpleDateFormat("yyMMddHHmmss");    
        String formatDate = format.format(new Date());    
            
        int random = new Random().nextInt(10000);    
            
            
        int position = fileName.lastIndexOf(".");    
        String extension = fileName.substring(position);    
            
        return formatDate + random + extension;    

    }      
	
	private File image;
	private String imageFileName;
	private Food food;
	private Method method;
	private Integer id;
	@Autowired
	private FoodService foodService; 
	@Autowired
	private FoodTypeService foodTypeService;
	@Autowired
	private MethodService methodService;
	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public void setFoodService(FoodService foodService) {
		this.foodService = foodService;
	}

	public void setFoodTypeService(FoodTypeService foodTypeService) {
		this.foodTypeService = foodTypeService;
	}

	public Food getFood() {
		return food;
	}

	public void setFood(Food food) {
		this.food = food;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setMethodService(MethodService methodService) {
		this.methodService = methodService;
	}

	public Method getMethod() {
		return method;
	}

	public void setMethod(Method method) {
		this.method = method;
	}
}
